package  
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		Loads the achievements data from AchievData.xml into the Vector of AchievementObjects that is passed in.
	 * 
	 */
	public class AchievementsLoader
	{
		private var achievementList:Vector.<AchievementObject>
		private var mainRef:Main;
		private var achievementsRef:Achievements;
		
		//embed the achievement XML
		[Embed(source="../src/AchievData.xml", mimeType="application/octet-stream")]
		protected const EmbeddedXML:Class;
		//private var _data:XML;
		//private var _data:XML = XML(new EmbeddedXML());
		
		
		public function AchievementsLoader(mainRef:Main, achievementsRef:Achievements, achievementList:Vector.<AchievementObject>) 
		{
			this.mainRef = mainRef;
			this.achievementsRef = achievementsRef;
			this.achievementList = achievementList;
			//var _XMLLoader:URLLoader = new URLLoader();
			//var _URLReq:URLRequest = new URLRequest("../src/AchievData.xml");
			//var _URLReq:URLRequest = new URLRequest("http://inscopegames.com/projects/piratecountdown/src/AchievData.xml");
			//
			//
			//mainRef.GetStartMenuRef().TestIfWorking("Loading achievements from:\n" + _URLReq.url);
			//_XMLLoader.load(_URLReq);
			//_XMLLoader.addEventListener(Event.COMPLETE, LoadAchievements);
			//
			var _contentFile:ByteArray = (new EmbeddedXML()) as ByteArray;
			var _contentString:String = _contentFile.readUTFBytes( _contentFile.length );
			var _data:XML = new XML(_contentString);
			
			LoadAchievements(_data);
			
		}
		
		//LoadAchievements()
		//Loads the achievements in the AchievData.xml into the vector achievementList that was passed into the class constructor
		private function LoadAchievements(_data:XML):void
		{
			//_data:XML is a private var of this class
			//var _data:XML = new XML(e.target.data);
			var _varCheck:int;
			var _quantityCheck:int;
			//var _comboArray:Array;							//holds combination of achievements to check against
			var _achObj:AchievementObject;
			trace("_data.ach.length():" + _data.ach.length());
			for (var i:int = 0; i < _data.ach.length(); i++)
			{
				//_varCheck = achievementsRef[_data.ach[i].@varcheck];
				if (_data.ach[i].@quantity.length() > 0) {
					_quantityCheck = int(_data.ach[i].@quantity);
				} else {
					_quantityCheck = 1;								//default value of 1 if not specified
				}
				
				
				_achObj = new AchievementObject(mainRef, achievementsRef, _data.ach[i].@id, _data.ach[i].@name, _data.ach[i].@desc, _data.ach[i].@varcheck, _quantityCheck);
				
				
				
				if (_data.ach[i].@combo.length() > 0) {
					//_comboArray = _data.ach[i].@combo.split(",");
					_achObj.InitComboAchievs(_data.ach[i].@combo.split(","));			//set the combination of achievements required to get this achievement
				}
				//trace(_data.ach[i].@id + ":" + _data.ach[i].@name + ":" + _data.ach[i].@desc + ":" + _data.ach[i].@varcheck + ":" + _quantityCheck + ":" + _data.ach[i].@combo);
				achievementList.push(_achObj);
				var number:int = achievementList.indexOf(_achObj);
				_achObj.achievNum = (number + 1);
			}
			
			//mainRef.GetStartMenuRef().TestIfWorking("...done loading achievements!");
			
			//mainRef.GetAchievMenuRef().OnXMLLoad();	//now tell achievmenu it's okay to load the achievements
		}
		
		
		/*
		//LoadAchievements()
		//Loads the achievements in the AchievData.xml into the vector achievementList that was passed into the class constructor
		private function LoadAchievements(e:Event):void
		{
			var _data:XML = new XML(e.target.data);
			var _varCheck:int;
			var _quantityCheck:int;
			//var _comboArray:Array;							//holds combination of achievements to check against
			var _achObj:AchievementObject;
			for (var i:int = 0; i < _data.ach.length(); i++)
			{
				//_varCheck = achievementsRef[_data.ach[i].@varcheck];
				if (_data.ach[i].@quantity.length() > 0) {
					_quantityCheck = int(_data.ach[i].@quantity);
				} else {
					_quantityCheck = 1;								//default value of 1 if not specified
				}
				_achObj = new AchievementObject(mainRef, achievementsRef, _data.ach[i].@id, _data.ach[i].@name, _data.ach[i].@desc, _data.ach[i].@varcheck, _quantityCheck)
				if (_data.ach[i].@combo.length() > 0) {
					//_comboArray = _data.ach[i].@combo.split(",");
					_achObj.InitComboAchievs(_data.ach[i].@combo.split(","));			//set the combination of achievements required to get this achievement
				}
				//trace(_data.ach[i].@id + ":" + _data.ach[i].@name + ":" + _data.ach[i].@desc + ":" + _data.ach[i].@varcheck + ":" + _quantityCheck + ":" + _data.ach[i].@combo);
				achievementList.push(_achObj);
			}
			
			mainRef.GetStartMenuRef().TestIfWorking("...done loading achievements!");
			
			//mainRef.GetAchievMenuRef().OnXMLLoad();	//now tell achievmenu it's okay to load the achievements
		}*/
	}

}